package com.example.plugin.util;

import com.example.plugin.config.GenerateModel;
import com.intellij.database.model.DasColumn;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 数据处理工具类
 *
 * @author lipan
 * @date 2022/1/16 21:01
 * @email 13160080997@163.com
 */
public class DataUtil {

    public static String convert(String dataBaseType,String columnType){
        if("mysql".equals(dataBaseType)){
            return convertMysql(columnType);
        }else if("postgresql".equals(dataBaseType)){
            return convertPostgresql(columnType);
        }
        return "String";
    }

    private static String convertPostgresql(String columnType) {
        if(columnType.contains("varchar")){
            return "String";
        }else if(columnType.contains("decimal")||columnType.contains("numberic")){
            return "BigDecimal";
        }else if(columnType.contains("datetime")||columnType.contains("timestamp")){
            return "Timestamp";
        }else if(columnType.contains("date")){
            return "Date";
        }else if(columnType.contains("int")){
            String substring = columnType.substring(columnType.indexOf("(") + 1, columnType.indexOf(")"));
            Integer value = Integer.valueOf(substring);
            if(value>10){
                return "Integer";
            }
            return "Long";
        }
        return "String";
    }

    public static String convertMysql(String columnType){
        if(columnType.contains("varchar")){
            return "String";
        }else if(columnType.contains("decimal")){
            return "BigDecimal";
        }else if(columnType.contains("datetime")){
            return "Timestamp";
        }else if(columnType.contains("date")){
            return "Date";
        }else if(columnType.contains("int")){
            String substring = columnType.substring(columnType.indexOf("(") + 1, columnType.indexOf(")"));
            Integer value = Integer.valueOf(substring);
            if(value>10){
                return "Integer";
            }
            return "Long";
        }
        return "String";
    }

    private static Pattern linePattern = Pattern.compile("_(\\w)");

    /**
     * 转驼峰
     * @param str
     * @return
     */
    public static String lineToHump(String str) {
        str = str.toLowerCase();
        Matcher matcher = linePattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    public static GenerateModel build(String dataBaseType,DasColumn column){
        String dataType = column.getDataType().toString();
        String comment = column.getComment();
        String columnName = column.getName().toLowerCase();
        String name = lineToHump(columnName);
        String type = convert(dataBaseType, dataType);
        GenerateModel generateModel = new GenerateModel(name,columnName,comment,type);
        return generateModel;
    }
}
